/*
  S.M.A.C.K - An operating system kernel
  Copyright (C) 2010,2011 Mattias Holm and Kristian Rietveld
  For licensing and a full list of authors of the kernel, see the files
  COPYING and AUTHORS.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <syscalls.h>

int
main(int argc, char **argv)
{
  int start = -1, end = -1;
  char buf[256];
  int buf_len;
  int result;
  int argv0_len = strnlen(argv[0], 256);

  getwd(buf, 256);

  /* Figure out where file name is located */
  for (int i = 0; i < argv0_len + 1; i++)
    {
      if (start == -1)
        {
          if (argv[0][i] == ' ')
            continue;

          start = i;
          continue;
        }
      else if (end == -1)
        {
          if (argv[0][i] != ' ' && argv[0][i] != 0)
            continue;

          end = i;
          break;
        }
    }

  if (start == -1 || end == -1)
    {
      printf("rmdir: error: no path provided.\n");
      return -1;
    }

  buf_len = strnlen(buf, 256);
  buf[buf_len] = '/';
  strlcpy(buf + buf_len + 1, argv[0] + start, end - start + 1);

  result = rmdir(buf);

  /* FIXME: We need a user-space errno.h */
  switch (result)
    {
      case -1: /* EINVAL */
        printf("rmdir: invalid path.\n");
        return result;

      case -3: /* ENOENT */
        printf("rmdir: path does not exist.\n");
        return result;

      case -4: /* ENOTDIR */
        printf("rmdir: path is not a directory.\n");
        return result;

      case -10: /* ENOTEMPTY */
        printf("rmdir: directory is not empty.\n");
        return result;
    }

  return 0;
}
